package flc.ast.fragment;

import android.content.pm.ActivityInfo;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.view.View;

import com.blankj.utilcode.util.SPUtils;
import com.blankj.utilcode.util.ScreenUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.ArrayList;
import java.util.List;

import flc.ast.R;
import flc.ast.activity.ScreenAppActivity;
import flc.ast.adapter.OrientationAdapter;
import flc.ast.bean.OrientationBean;
import flc.ast.databinding.FragmentScreenManageBinding;
import flc.ast.utils.Content;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.event.EventStatProxy;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link ScreenManageFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class ScreenManageFragment extends BaseNoModelFragment<FragmentScreenManageBinding> {

    private OrientationAdapter orientationAdapter;
    private int oldPosition;

    @Override
    protected int onCreate() {
        return R.layout.fragment_screen_manage;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);
        EventStatProxy.getInstance().statEvent5(getActivity(), mDataBinding.event5Container);

        mDataBinding.tvSet.setOnClickListener(this);
        mDataBinding.ivToastSwitch.setOnClickListener(this);
        mDataBinding.ivOrientationSwitch.setOnClickListener(this);
        mDataBinding.ivExtendSwitch.setOnClickListener(this);
        mDataBinding.ivLockSwitch.setOnClickListener(this);

        mDataBinding.rvOrientation.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        orientationAdapter = new OrientationAdapter();
        mDataBinding.rvOrientation.setAdapter(orientationAdapter);
        orientationAdapter.setOnItemClickListener(this);
    }

    @Override
    protected void onClickCallback(View v) {
        int id = v.getId();
        if (id == R.id.tvSet) {
            startActivity(ScreenAppActivity.class);
        } else if (id == R.id.ivOrientationSwitch) {
            boolean orientationSwitch = SPUtils.getInstance().getBoolean(Content.APP_ORIENTATION_KEY, false);
            orientationSwitch = !orientationSwitch;
            mDataBinding.ivOrientationSwitch.setSelected(orientationSwitch);
            mDataBinding.tvOrientationTips.setText(orientationSwitch ? getString(R.string.fx_on_tips) : getString(R.string.fx_off_tips));
            SPUtils.getInstance().put(Content.APP_ORIENTATION_KEY, orientationSwitch);
        } else if (id == R.id.ivExtendSwitch) {
            boolean extendSwitch = SPUtils.getInstance().getBoolean(Content.EXTENDS_KEY, false);
            extendSwitch = !extendSwitch;
            mDataBinding.ivExtendSwitch.setSelected(extendSwitch);
            SPUtils.getInstance().put(Content.EXTENDS_KEY, extendSwitch);
        } else if (id == R.id.ivLockSwitch) {
            boolean lockSwitch = SPUtils.getInstance().getBoolean(Content.LOCK_ORIENTATION_KEY, false);
            lockSwitch = !lockSwitch;
            mDataBinding.ivLockSwitch.setSelected(lockSwitch);
            mDataBinding.tvLockTips.setText(lockSwitch ? getString(R.string.lock_on_tips) : getString(R.string.lock_off_tips));
            SPUtils.getInstance().put(Content.LOCK_ORIENTATION_KEY, lockSwitch);
        } else if (id == R.id.ivToastSwitch) {
            boolean toastSwitch = SPUtils.getInstance().getBoolean(Content.TOAST_KEY, true);
            toastSwitch = !toastSwitch;
            mDataBinding.ivToastSwitch.setSelected(toastSwitch);
            mDataBinding.tvToast1.setText(toastSwitch ? getString(R.string.show_toast_tips) : getString(R.string.bxs_toast_text));
            SPUtils.getInstance().put(Content.TOAST_KEY, toastSwitch);
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        orientationAdapter.getItem(oldPosition).setSelected(false);
        oldPosition = position;
        orientationAdapter.getItem(position).setSelected(true);
        orientationAdapter.notifyDataSetChanged();
        clickScreenOrientation(position);
    }

    @Override
    protected void initData() {
        setData();
    }

    private void setData() {
        List<OrientationBean> list = new ArrayList<>();
        list.add(new OrientationBean(R.drawable.a1, R.drawable.b1, true));
        list.add(new OrientationBean(R.drawable.a2, R.drawable.b2, false));
        list.add(new OrientationBean(R.drawable.a3, R.drawable.b3, false));
        list.add(new OrientationBean(R.drawable.a4, R.drawable.b4, false));
        list.add(new OrientationBean(R.drawable.a5, R.drawable.b5, false));
        list.add(new OrientationBean(R.drawable.a6, R.drawable.b6, false));
        list.add(new OrientationBean(R.drawable.a7, R.drawable.b7, false));
        list.add(new OrientationBean(R.drawable.a8, R.drawable.b8, false));
        orientationAdapter.setList(list);
    }

    private void clickScreenOrientation(int position) {
        switch (position) {
            case 0:
                getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
                if (SPUtils.getInstance().getBoolean(Content.TOAST_KEY)) {
                    ToastUtils.showShort(R.string.set_success_tips);
                }
                break;
            case 1:
                ScreenUtils.setPortrait(getActivity());
                break;
            case 2:
                ScreenUtils.setLandscape(getActivity());
                break;
            case 3:
                getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
                break;
            case 4:
                getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
                break;
            case 5:
                getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_BEHIND);
                if (SPUtils.getInstance().getBoolean(Content.TOAST_KEY)) {
                    ToastUtils.showShort(R.string.set_success_tips);
                }
                break;
            case 6:
            case 7:
            case 8:
                if (SPUtils.getInstance().getBoolean(Content.TOAST_KEY)) {
                    ToastUtils.showShort(R.string.set_success_tips);
                }
                break;
            default:
                break;
        }
    }

}